package ar.edu.caece.compiler.grammar.factories;

import ar.edu.caece.compiler.error.ParsingException;
import ar.edu.caece.compiler.grammar.AbstractTokenVisitorFactory;
import ar.edu.caece.compiler.grammar.TokenRecognizer;
import ar.edu.caece.compiler.grammar.TokenVisitor;
import ar.edu.caece.compiler.grammar.nonterminals.POSID;
import ar.edu.caece.compiler.grammar.nonterminals.POSEXPRLLAM1;
import ar.edu.caece.compiler.grammar.terminals.IdentifierToken;
import ar.edu.caece.compiler.grammar.Recognizer;

public class POSEXPRLLAM1Factory extends AbstractTokenVisitorFactory {

	private static POSEXPRLLAM1Factory INSTANCE;
	
	private POSIDFactory posidFactory;
	private TokenVisitor parsePOSID;
	
	public static POSEXPRLLAM1Factory getInstance() throws Exception {
		if(INSTANCE==null) INSTANCE = new POSEXPRLLAM1Factory();
		return INSTANCE;
	}
	
	private Initializer initializer = new Initializer() {
		
		@Override
		public void init() throws Exception {
			initializer = NoOperationInitializer.getInstance();
			posidFactory = POSIDFactory.getInstance();
			parsePOSID = posidFactory.makeVisitor();
		}
	};
	
	private POSEXPRLLAM1Factory() throws Exception {

	}
	
	@Override
	public TokenVisitor makeChainedVisitor(
			final TokenVisitor siguiente) throws Exception {
		initializer.init();

		return new IdentifierToken.IdentifierChainVisitor(siguiente) {
			
			@Override
			public Recognizer identifierCase(IdentifierToken llamador) throws Exception {
				POSID posid = (POSID) nextToken().execute(parsePOSID);
				POSEXPRLLAM1 posexprllam1 = new POSEXPRLLAM1(llamador, posid);
				return posexprllam1;
			}
		};
	
	}
	@Override
	public TokenVisitor makeVisitor() throws Exception {
		initializer.init();

		return new IdentifierToken.IdentifierTokenVisitor() {
			
			@Override
			public Recognizer defaultCase(TokenRecognizer llamador) throws Exception {
				throw new ParsingException("Error de parsing.",POSEXPRLLAM1.class,llamador);
			}
			
			@Override
			public Recognizer identifierCase(IdentifierToken llamador) throws Exception {
				POSID posid = (POSID) nextToken().execute(parsePOSID);
				POSEXPRLLAM1 posexprllam1 = new POSEXPRLLAM1(llamador, posid);
				return posexprllam1;
			}
		};
	}
	
	
}
